package models;



import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

import play.db.jpa.Blob;
import play.db.jpa.Model;

@Entity

public class Student extends Model {
	
	public String fname;
	public String lname;
	public String mname;
	public Date dateOfBirth;
	public String homeAddress;
	public String homePhone;
	public boolean gender;
	
	public String MembershipID;
	
	@Column(unique = true)
	public String barcode;
	
	public String email;
	
	public String mobilePhone;
        
    public String email2;
	
	public String mobilePhone2;
	
	@OneToMany(mappedBy="student",cascade= CascadeType.ALL)
	public List<Register> registrations;
	
	public Blob photo;

	public Student(String fname, String lname, String mname, Date dateOfBirth,
			String homeAddress, String homePhone, boolean gender,
			String membershipID, String email, String mobilePhone) {
		
		this.fname = fname;
		this.lname = lname;
		this.mname = mname;
		this.dateOfBirth = dateOfBirth;
		this.homeAddress = homeAddress;
		this.homePhone = homePhone;
		this.gender = gender;
		this.MembershipID = membershipID;
		this.email = email;
		this.mobilePhone = mobilePhone;
		registrations  = new LinkedList<Register>();
		
	}

	public void generateBarcode() {
		
//		String plaintext = id+"";
//		MessageDigest m = null;
//		try {
//			m = MessageDigest.getInstance("MD5");
//		
//		} catch (NoSuchAlgorithmException e) {
//			
//			e.printStackTrace();
//		}
//		
//		m.reset();
//		m.update(plaintext.getBytes());
//		byte[] digest = m.digest();
//		BigInteger bigInt = new BigInteger(1,digest);
//		String hashtext = bigInt.toString(16);
//		while(hashtext.length() < 32 ){
//		  hashtext = "0"+hashtext;
//		}
//		barcode = hashtext;
		
		Date d = new Date();
		@SuppressWarnings("deprecation")
		String thisYear = ""+d.getYear();
		barcode = thisYear + id;
		
	}
	
	public String getName() {
		
		return ""+fname+" "+mname+" "+lname;
		
	}
	
	public boolean hasPhoto() {
		try {
			
			return(photo.getFile().isFile());
		
		}
		catch (Exception e) {
			return false;
		}
	}
	
	

}
